package com.yazi.mxz.common.service.impl;


import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.exception.MyNoPermissionException;
import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yazi.mxz.common.ComSetting;
import com.yazi.mxz.common.db.dao.CompanyDao;
import com.yazi.mxz.common.db.dao.StoreRegionDao;
import com.yazi.mxz.common.db.dao.MemberDao;
import com.yazi.mxz.common.db.dao.RoleDao;
import com.yazi.mxz.common.db.dao.StoreDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.Role;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreRegion;
import com.yazi.mxz.common.entity.beans.StoreRegionId;
import com.yazi.mxz.common.entity.enums.RoleType;
import com.yazi.mxz.common.entity.enums.SettingId;
import com.yazi.mxz.common.service.StoreService;

@Service("storeService")
public class StoreServiceImpl extends BasicService<Store> implements StoreService {
	private static Logger log = LoggerFactory.getLogger(StoreServiceImpl.class);
	
	@Resource
	StoreDao storeDao;
	@Resource
	MemberDao memberDao;
	@Resource
	RoleDao roleDao;
	@Resource
	StoreRegionDao storeRegionDao;
	@Resource
	CompanyDao companyDao;
	
	@Override
	protected Dao<Store> getDao() {
		return storeDao;
	}
	
	@Override 
	public Store addByCompany(Company company, Store store) throws MyException {
		log.debug("addByCompany：companyId={},storeName={}", company.getCompanyId(), store.getName());
		
		// 数量检查
		if (company.getStoreRemain() <= 0) {
			throw new MyNoPermissionException("数量受限");
		}
		company.setStoreRemain(company.getStoreRemain() - 1);
		companyDao.merge(company);
		
		store = ValueFactory.validate(store);
		store.setPutupRemain(ComSetting.getInteger(SettingId.STORE_OFFLINESALE_AMOUNT));
		store.setPromRemain(ComSetting.getInteger(SettingId.STORE_PROMOTE_AMOUNT));
		store.setMemRemain(ComSetting.getInteger(SettingId.STORE_MEMBER_AMOUNT));
		store.setLocalSetting(ComSetting.getValue(SettingId.FIELD_SALE_SETTING));
		store.setCompany(company);
		store.setIsOpen(true);// TODO
		store.setIsOpenLocal(true);// TODO
		store.setIsOpenOnline(true);// TODO
		store.setGoodsScore(0f);// TODO
		store.setServiceScore(0f);// TODO
		store.setDeliveryScore(0f);// TODO
		store.setIsRecommand(false);
		store.setRecommandSort(0l);
		store.setUpdateTime(DateUtil.getTimeStamp());
		store.setAddTime(DateUtil.getTimeStamp());
		store.setStoreId(null);
		storeDao.persist(store);
		
		// TODO 店铺管理员角色
		Role role = new Role();
		ValueFactory.validate(role);
		role.setStore(store);
		role.setName("管理员");
		role.setPerms("*");
		role.setType(RoleType.SHOP.ordinal());
		role.setRoleId(null);
		roleDao.persist(role);
		
		// TODO 店铺营业员角色
		Role role2 = new Role();
		ValueFactory.validate(role2);
		role2.setStore(store);
		role2.setName("营业员");
		role2.setPerms("storeMember:none;goodsReport:none");
		role2.setType(RoleType.SHOP.ordinal());
		role2.setRoleId(null);
		roleDao.persist(role2);
		
		// 将店铺地区作为店铺第一个配送地区
		StoreRegionId srId = new StoreRegionId();
		srId.setStoreId(store.getStoreId());
		srId.setRegionId(store.getRegion().getRegionId());
		StoreRegion sr = new StoreRegion();
		sr.setId(srId);
		sr.setRegionPath(store.getRegionPath());
		storeRegionDao.persist(sr);
		
		return store;
	}
	
	@Override
	public Store editByCompany(Company company, Store store) throws MyException {
		log.debug("editByCompany：companyId={},storeId={}", company.getCompanyId(), store.getStoreId());
		
		Store destStore = storeDao.get(store.getStoreId());
		if (!destStore.getCompany().equals(company)) {
			throw new MyNoPermissionException();
		}
		
		// 防止篡改以下信息
		limitFieldsForMemberForm(store);
		
		destStore = ValueFactory.combine(store, destStore);
		destStore.setUpdateTime(DateUtil.getTimeStamp());
		return storeDao.merge(destStore);
	}
	
	@Override
	public Store editByStore(Store from, Store to) {
		log.debug("editByStore：storeId={}", to.getStoreId());
		
		// 防止篡改以下信息
		limitFieldsForMemberForm(from);
		
		ValueFactory.combine(from, to);
		to.setUpdateTime(DateUtil.getTimeStamp());
		return storeDao.merge(to);
	}

	/**
	 * 过滤出禁止用户操作的字段属性
	 * @param store
	 */
	private void limitFieldsForMemberForm(Store store) {
		store.setCompany(null);
		store.setMemRemain(null);
		store.setPromRemain(null);
		store.setPutupRemain(null);
		store.setIsOpenOnline(null);
		store.setIsOpenLocal(null);
		store.setGoodsScore(null);
		store.setServiceScore(null);
		store.setDeliveryScore(null);
		store.setIsRecommand(null);
		store.setRecommandSort(null);
		store.setAddTime(null);
	}
}
